package com.hz.hongbao.fragments;

import android.content.Context;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;

import com.hz.hongbao.R;


/**
 * Created by Zhongyi on 2/4/16.
 */
public class GeneralSettingsFragment extends PreferenceFragment {
    private Context mContext;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.general_preferences);
        mContext = getActivity();

        setPrefListeners();
    }

    private void setPrefListeners() {

        /*//推荐热门应用
        Preference tuijianPref = findPreference("pref_tuijian");
        tuijianPref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            public boolean onPreferenceClick(Preference preference) {
                int num = new Random().nextInt(2);
                if(num == 1){
                    // 显示推荐列表（软件）
//                    AppConnect.getInstance(mContext).showAppOffers(mContext);
                }else{
                    // 显示推荐列表（游戏）
//                    AppConnect.getInstance(mContext).showGameOffers(mContext);
                }
                return false;
            }
        });

        // Check for updates
        Preference updatePref = findPreference("pref_etc_check_update");
        updatePref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            public boolean onPreferenceClick(Preference preference) {
                //检查更新
//                AppConnect.getInstance(mContext).checkUpdate(mContext);
                return false;
            }
        });

        //反馈
        Preference issuePref = findPreference("pref_etc_issue");
        issuePref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            public boolean onPreferenceClick(Preference preference) {
                // 用户反馈
//                AppConnect.getInstance(mContext).showFeedback(mContext);
                return false;
            }
        });*/

        Preference excludeWordsPref = findPreference("pref_watch_exclude_words");
        String summary = getResources().getString(R.string.pref_watch_exclude_words_summary);
        String value = PreferenceManager.getDefaultSharedPreferences(getActivity()).getString("pref_watch_exclude_words", "");
        if (value.length() > 0) excludeWordsPref.setSummary(summary + ":" + value);

        excludeWordsPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object o) {
                String summary = getResources().getString(R.string.pref_watch_exclude_words_summary);
                if (o != null && o.toString().length() > 0) {
                    preference.setSummary(summary + ":" + o.toString());
                } else {
                    preference.setSummary(summary);
                }
                return true;
            }
        });
    }
}
